package com.aistar.controller;

import com.aistar.pojo.*;
import com.aistar.service.ProOrderService;
import com.aistar.service.ProductService;
import com.aistar.service.TravellerService;
import com.aistar.util.DateUtil;
import com.aistar.util.LeeJSONResult;
import com.aistar.util.OrderStatusEnum;
import com.aistar.util.ServerResponse;
import com.google.gson.JsonArray;
import com.qiniu.util.Json;
import com.qiniu.util.StringMap;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class OrderController {
    @Autowired
    ProOrderService proOrderService;
    @Autowired
    ProductService productService;
    @Autowired
    TravellerService travellerService;

    @GetMapping("/order/getId")
    @ResponseBody
    private ServerResponse torder(HttpSession session) {

        Customer customer = (Customer) session.getAttribute("loginCustomer");
        Integer cusId = customer.getId();

        return ServerResponse.getDataSuccess(cusId);
    }

    /**
     * 根据i用户id查询订单
     *
     * @return
     */
    @GetMapping("/order/customer/{id}")
    @ResponseBody
    private ServerResponse getOrdersById(@PathVariable("id") Integer custId) {
        System.out.println(custId);
        ServerResponse serverResponse = proOrderService.getAll(custId);
        return serverResponse;
    }

    @RequestMapping("/order/order")
    private String order() {
        return "order/order";
    }

    @RequestMapping("/order/detail")
    private String orderDetail() {
        return "order/order_detail";
    }

    //添加订单
    @PostMapping("/order/add")
    @ResponseBody
    public LeeJSONResult addOrder(@RequestParam String customeId, @RequestParam String productId, @RequestParam String startDate, @RequestParam String personNumber,
                                  @RequestParam String childNumber, @RequestParam String orderMoney, @RequestParam String orderName, @RequestParam String travellerList) {
        Date endDate = DateUtil.string2Date(startDate);
        Product product = (Product) productService.getById(Integer.valueOf(productId)).getData();

        endDate = DateUtil.getBeforeDate(-(Integer.valueOf(product.getProductDays())));
        ProOrder proOrder = new ProOrder();
        Integer orderId = (Integer) proOrderService.getNewOrderId().getData();
        proOrder.setId(orderId);
        proOrder.setEnddate(endDate);
        proOrder.setCustomerId(Integer.valueOf(customeId));
        proOrder.setProductId(Integer.valueOf(productId));
        proOrder.setChildNumber(Integer.valueOf(childNumber));
        proOrder.setPersonNumber(Integer.valueOf(personNumber));
        proOrder.setOrderMoney(Float.valueOf(orderMoney));
        proOrder.setOrderName(orderName);
        proOrder.setStartdate(DateUtil.string2Date(startDate));
        proOrder.setCreateDate(new Date());
        proOrder.setOrderType("0");
        proOrder.setOrderStatus(OrderStatusEnum.WAIT_PAY.value);
        proOrder.setStatus("1");
        proOrder.setOrderNo(UUID.randomUUID().toString().replace("-", ""));

        proOrderService.addOrder(proOrder);
        //添加旅客信息
        JSONArray jsonResult = JSONArray.fromObject(travellerList);
        for (int i = 0; i < jsonResult.size(); i++) {
            JSONObject jsonObject = jsonResult.getJSONObject(i);
            Traveller traveller = new Traveller();
            //设置用户id
            traveller.setCustomerId(Integer.valueOf(customeId));
            //设置旅客姓名
            traveller.setTravellerName(jsonObject.getString("travellerName"));
            traveller.setCustomerId(Integer.valueOf(customeId));
            traveller.setCertificateType(jsonObject.getString("certificateType"));
            String certificateNo = jsonObject.getString("certificateNo");
            traveller.setCertificateNo(certificateNo);
            if (certificateNo.length() == 15) {
                if (Integer.valueOf(certificateNo.charAt(14)) % 2 != 0)
                    traveller.setTravellerGender("男");
                else
                    traveller.setTravellerGender("女");

            } else {
                if (Integer.valueOf(certificateNo.charAt(16)) % 2 != 0)
                    traveller.setTravellerGender("男");
                else
                    traveller.setTravellerGender("女");
            }
            traveller.setTravellerType(jsonObject.getString("travellerType"));
            traveller.setStatus("1");
            System.out.println("traveller:" + traveller);
            travellerService.addTrave(traveller);
            //将出行旅客信息加入数据库
            Tripper tripper =new Tripper();
            tripper.setOrderId(orderId);

        }


        //添加旅客信息到旅客订单表
        return LeeJSONResult.ok(orderId);
    }


    @GetMapping("/order_detail/{orderId}/{proId}")
    @ResponseBody
    public ServerResponse getOrderDetail(@PathVariable("orderId") Integer orderId,
                                         @PathVariable("proId") Integer proId) {
        ServerResponse serverResponse = proOrderService.getOrderDetail(orderId, proId);
        return serverResponse;
    }
}

